@extends('layouts.admin_common') @section('content')

    @if(app('env')=='production')
        <script src="{{ secure_asset('/js/sy_common.js') }}"></script>
    @else
        <script src="{{ asset('/js/sy_common.js') }}"></script>
    @endif
    <div class="breadcrumbs_container">{{
	Breadcrumbs::render('admin.ship') }}</div>
    <div class="system_tab_line"></div>

    <div class="lyx_search_table">

        <div class="row">
            <table class="table table-bordered">
                <thead>
                <tr>
                    <th scope="col">@lang('symessages.info.order_code')</th>
                    <th scope="col">@lang('symessages.info.order_date')</th>
                    <th scope="col">@lang('symessages.info.company_shop')</th>
                    <th scope="col">@lang('symessages.info.post_address')</th>
                    <th scope="col">@lang('symessages.info.company_mobile')</th>
                    <th scope="col">@lang('symessages.info.total_money')</th>
                    <th scope="col">@lang('symessages.ajax_data.courier_number')</th>
                    <th scope="col">@lang('symessages.info.clearing_from')</th>
                </tr>
                </thead>
                <tbody>
                <tr>
                    <td>
                        <select class="form-control form-control-sm" id="lyx_order_select">
                            <option value="">select purchase order</option>
                            @foreach($orderList as $item)
                                <option value="{{$item->order_id}}" data-info="{{json_encode($item)}}">{{$item->order_id}}</option>
                            @endforeach
                        </select>
                    </td>
                    <td class="t_order_date">

                    </td>
                    <td class="t_company_shop"></td>
                    <td class="t_post_address"></td>
                    <td class="t_company_mobile"></td>
                    <td class="t_total_money"></td>
                    <td class="t_courier_number"><input class="form-control form-control-sm t_post_code" type="text" placeholder=""></td>
                    <td class="t_clearing_from"></td>
                </tr>


                </tbody>
            </table>
        </div>

        <div class="lyx_table row">

            <table class="table table-bordered">
                <thead>
                <tr>
                    <th scope="col">@lang('symessages.info.product_code')</th>
                    <th scope="col">@lang('symessages.info.product_name')</th>
                    <th scope="col">@lang('symessages.info.size')</th>
                    <th scope="col">@lang('symessages.info.store_num')</th>
                    <th scope="col">@lang('symessages.info.price')</th>
                    <th scope="col">@lang('symessages.info.order_goods_num')</th>
                    <th scope="col">@lang('symessages.info.total_money')</th>
                    <th scope="col">@lang('symessages.info.out_num')</th>
                    <th scope="col">@lang('symessages.info.last_repertory')</th>
                </tr>
                </thead>
                <tbody id="lyx_tbody">
                {{--<tr>--}}
                    {{--<td>--}}
                        {{--before select category--}}
                    {{--</td>--}}
                    {{--<td></td>--}}
                    {{--<td></td>--}}
                    {{--<td></td>--}}
                    {{--<td width="100px">--}}
                        {{--<input class="form-control form-control-sm lyx_house_num" type="number" placeholder="">--}}
                    {{--</td>--}}
                    {{--<td></td>--}}
                    {{--<td></td>--}}
                {{--</tr>--}}

                {{--<tr id="lyx_total_tr">--}}
                    {{--<td colspan="6" style="text-align: center">合計金額</td>--}}
                    {{--<td colspan="2" id="total_sum_money">¥0</td>--}}
                {{--</tr>--}}

                </tbody>
            </table>

        </div>
        <div class="row">
            <div class="col-sm-2"></div>
            <div class="col-sm-3">
                {{--<button class="btn btn-primary button_normal">Csv取込</button>--}}
            </div>
            <div class="col-sm-3">
                <button class="btn btn-primary button_normal" id="lyx_post_submit">确定</button>
            </div>
            <div class="col-sm-3">
                {{--<button class="btn btn-primary button_normal">WEB反映</button>--}}
            </div>
        </div>
    </div>

    </div>

<script>

    var clearing_form = <?php echo json_encode(clearing_from(),JSON_UNESCAPED_UNICODE) ?>;

    $('#lyx_post_submit').on('click',function () {
        postData();
    })

    order_select_action();

    function order_select_action() {
        $("#lyx_order_select").off().change(function () {
            var value = $(this).val();
            var that = $(this).parents('tr');
            var info = $(this).find("option:selected").data('info');

            that.find('.t_order_date').html(info.create_time);
            that.find('.t_company_shop').html(info.name);
            that.find('.t_post_address').html(info.add);
            that.find('.t_company_mobile').html(info.tel);
            that.find('.t_total_money').html(info.money);
            that.find('.t_clearing_from').html(clearing_form[info.clearing_from]);



            var that = $('#lyx_tbody');
            if (value == null || value == '' || value == undefined) {
                that.html('<tr><td colspan="10" style="text-align: center">empty</td></tr>');
            } else {
                that.html('<tr><td colspan="10" style="text-align: center">loading</td></tr>');
                get_user_order_detailed_list(value);
            }
        });
    }

    /**
     * 根据商户订单号获取商户的列表数据
     * @param order_id
     */
    function get_user_order_detailed_list(order_id) {
        hdAjax({
            url:'get_user_order_detailed_list',
            data:{order_id:order_id},
            success:function (retData) {
                var that = $('#lyx_tbody');
                if(retData.code == 1001){
                    if(retData.data.length > 0){
                        var html = '';
                        $.each(retData.data,function (index,item) {
                            html = html+html_row(item);
                        })
                        that.html(html);
                        num_action();
                    }else{
                        that.html('<tr><td colspan="10" style="text-align: center">empty data !</td></tr>');
                    }
                }else{
                    that.html('<tr><td colspan="10" style="text-align: center">'+retData.msg+'</td></tr>');
                }
            }
        })
    }
    
    
    function html_row(data) {
        var total_money = parseInt(data.goods_price) * parseInt(data.goods_num);
        var html = ['<tr data-info=\''+JSON.stringify(data)+'\'>',
            '                    <td>'+data.order_id+'</td>',
            '                    <td>'+data.goods_name+'</td>',
            '                    <td>'+data.color+'/'+data.size+'</td>',
            '                    <td>'+data.total_num+'</td>',
            '                    <td>'+data.goods_price+'</td>',
            '                    <td>'+data.goods_num+'</td>',
            '                    <td>'+total_money+'</td>',
            '                    <td width="100px"><input class="form-control form-control-sm lyx_house_num" type="number" placeholder=""></td>',
            '                    <td class="last_num">'+data.total_num+'</td>',
            '                </tr>'].join("");
        return html;
    }
    /**
     * 输入数字的事件
     */
    function num_action() {
        $(".lyx_house_num").off().change(function () {
            var value = $(this).val();
            var that = $(this).parents('tr');
            in_house_num_action(value,that);
        })
    }
    /**
     *
     * @param value 填写的数值
     * @param that tr的Dom
     */
    function in_house_num_action(value,that) {
        var info = that.data('info');
        that.data('num',value);
        that.find('.last_num').html(parseInt(info.total_num)-parseInt(value));

    }

    function postData() {

        var t_post_code = $(".t_post_code").val();
        if(t_post_code == '' || t_post_code == undefined || t_post_code == null) {
            layuiTopMsg('@lang('symessages.ajax_data.express_waybill_code')');
            return false;
        }

        var goodsList = [];
        $('#lyx_tbody tr').each(function () {
           var info = $(this).data('info');
           var num = $(this).data('num');
           var json = {order_id:info.order_id,goods_id:info.goods_id,goods_num:num,price:info.goods_price,model_info:{goods_model_id:info.model_id,size_name:info.size,color_name:info.color}};
           goodsList.push(json);
        })

        var order_id = $("#lyx_order_select").val();


        hdAjax({
            url:'user_out_goods_from_house',
            data:{goods_list:goodsList,express_waybill_code:t_post_code,order_id:order_id},
            type:'post',
            success:function (retData) {
                if(retData.code == 1001){
                    successAlert();
                }else{
                    layuiTopMsg(retData.msg);
                }
            }
        })


    }

    
</script>
@endsection

